package com.webcamtracker.tracker.example.spatial;


import javax.media.j3d.Behavior;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3f;
import java.util.Enumeration;

/**
 * TrackTransformation is a Tracking behavior object that lets users control the
 * rotation of an object via a TrackingEvent.
 * <p/>
 * To use this utility, first create a transform group that this
 * rotate behavior will operate on. Then,
 * <blockquote><pre>
 * <p/>
 *   TrackTransformation rotation = new TrackTransformation();
 *   rotation.setTransformGroup(objTrans);
 *   objTrans.addChild(rotation);
 *   rotation.setSchedulingBounds(bounds);
 * <p/>
 * </pre></blockquote>
 * The above code will add the rotate behavior to the transform
 * group. The user can rotate any object attached to the objTrans.
 */

public class TrackTransformation
        extends Behavior {
    private float x_factor = .05F;
    private float y_factor = .05F;

    protected int x_last, y_last;
    private float z_last;

    protected TransformGroup transformGroup;
    protected boolean buttonPress = false;
    protected boolean reset = false;

    public TrackTransformation(TransformGroup transformGroup) {
        this.transformGroup = transformGroup;
        reset = true;
        initialize();
    }

    /**
     * Return the x-axis movement multipler.
     *
     * @return double
     */
    public double getXFactor() {
        return x_factor;
    }

    /**
     * Return the y-axis movement multipler.
     *
     * @return double
     */
    public double getYFactor() {
        return y_factor;
    }

    /**
     * Set the x-axis amd y-axis movement multipler with factor.
     *
     * @param factor multipler
     */
    public void setFactor(float factor) {
        x_factor = y_factor = factor;
    }

    /**
     * Set the x-axis amd y-axis movement multipler with xFactor and yFactor
     * respectively.
     *
     * @param xFactor x multipler
     * @param yFactor y multipler
     */
    public void setFactor(float xFactor, float yFactor) {
        x_factor = xFactor;
        y_factor = yFactor;
    }

    public void doProcess(int x, int y, float z) {
        if (x_last != x || y_last != y || z_last != z) {
            Vector3f vector = new Vector3f(x * x_factor, y * y_factor, z - 4);
            Transform3D currXform = new Transform3D();

            //transformGroup.getTransform(currXform);

            currXform.setTranslation(vector);

            // Update xform
            transformGroup.setTransform(currXform);

            transformChanged(currXform);

            x_last = x;
            y_last = y;
            z_last = z;
        }
    }

    public void initialize() {
        x_last = 0;
        y_last = 0;
    }

    public void processStimulus(Enumeration enumeration) {
        System.out.println("TrackTransformation.processStimulus!!!!!!!!!! : " + enumeration);
    }

    /**
     * Users can overload this method  which is called every time
     * the Behavior updates the transform
     * <p/>
     * Default implementation does nothing
     *
     * @param transform Transformation applied to the transformGroup
     */
    public void transformChanged(Transform3D transform) {
    }
}
